package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.ILevelBizService;
import com.kk.xhr.admin.controller.vo.LevelVO;
import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.web.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 职位等级管理控制器
 *
 * @author kkmystery
 * @version 1.0 2021/3/5
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "等级管理", tags = "等级管理")
@RequestMapping("/level")
public class LevelController {
    private final Executor adminControllerBizExecutor;
    private final ILevelBizService levelBizService;

    @ApiOperation(value = "查询等级")
    @RequiresRoles(value = {"admin"})
    @GetMapping
    public CompletionStage<ApiResult<List<LevelVO>>> query() {
        return CompletableFuture.supplyAsync(() -> ApiResult.success(levelBizService.query()), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加等级")
    @RequiresRoles(value = {"admin"})
    @PostMapping
    public CompletionStage<ApiResult<Void>> add(@NotBlank String name) {
        return CompletableFuture.supplyAsync(() -> {
            levelBizService.add(name);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改等级")
    @RequiresRoles(value = {"admin"})
    @PutMapping("/{id:.+}")
    public CompletionStage<ApiResult<Void>> modify(@PathVariable("id") Long id, String name) {
        return CompletableFuture.supplyAsync(() -> {
            levelBizService.modify(id, name);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改等级状态")
    @RequiresRoles(value = {"admin"})
    @PutMapping("/status/{id:.+}")
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, BinaryEnum status) {
        return CompletableFuture.supplyAsync(() -> {
            levelBizService.modifyStatus(id, status);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
